﻿Public Class ProgressWindow

    Private Property CurrentStep As Integer = 0
    Private Property LastWasSkip As Boolean = False

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        ProgBar.Maximum = StepsGrid.RowDefinitions.Count + 1
    End Sub

    Public Sub StartNextStep()
        Dispatcher.Invoke(New ProgressDelegate(AddressOf _StartNextStep))
    End Sub

    Public Sub FinishCurrentStep()
        Dispatcher.Invoke(New FinishStepDelegate(AddressOf _FinishCurrentStep))
    End Sub

    Public Sub CurrentStepFailed(reason As String, Optional fullError As String = "Unknown Error")
        Dispatcher.Invoke(New CurrentStepFailedDelegate(AddressOf _CurrentStepFailed), {reason, fullError})
    End Sub

    Public Sub SkipNextStep()
        Dispatcher.Invoke(New SkipDelegate(AddressOf _SkipNextStep))
    End Sub

    Public Sub Success()
        Dispatcher.Invoke(New SuccessDelegate(AddressOf _Success))
    End Sub


    Private Sub _StartNextStep()
        ProgBar.Value += 1

        Dim tb = DirectCast((From child In StepsGrid.Children Where CInt(DirectCast(child, UIElement).GetValue(Grid.RowProperty)) = CurrentStep AndAlso CInt(DirectCast(child, UIElement).GetValue(Grid.ColumnProperty)) = 1 Select child)(0), TextBlock)
        Dim l = DirectCast((From child In StepsGrid.Children Where CInt(DirectCast(child, UIElement).GetValue(Grid.RowProperty)) = CurrentStep AndAlso CInt(DirectCast(child, UIElement).GetValue(Grid.ColumnProperty)) = 0 Select child)(0), Image)

        tb.FontWeight = FontWeights.Bold
        Dim uriSource = New Uri("Resources\iconArrow.png", UriKind.Relative)
        l.Source = New BitmapImage(uriSource)
    End Sub
    Delegate Sub ProgressDelegate()

    Private Sub _FinishCurrentStep()
        Dim tb = DirectCast((From child In StepsGrid.Children Where CInt(DirectCast(child, UIElement).GetValue(Grid.RowProperty)) = CurrentStep AndAlso CInt(DirectCast(child, UIElement).GetValue(Grid.ColumnProperty)) = 1 Select child)(0), TextBlock)
        Dim l = DirectCast((From child In StepsGrid.Children Where CInt(DirectCast(child, UIElement).GetValue(Grid.RowProperty)) = CurrentStep AndAlso CInt(DirectCast(child, UIElement).GetValue(Grid.ColumnProperty)) = 0 Select child)(0), Image)

        tb.FontWeight = FontWeights.Normal
        Dim uriSource = New Uri("Resources\iconTick.png", UriKind.Relative)
        l.Source = New BitmapImage(uriSource)

        CurrentStep += 1
    End Sub
    Delegate Sub FinishStepDelegate()


    Private Sub _SkipNextStep()
        ProgBar.Value += 1

        Dim tb = DirectCast((From child In StepsGrid.Children Where CInt(DirectCast(child, UIElement).GetValue(Grid.RowProperty)) = CurrentStep AndAlso CInt(DirectCast(child, UIElement).GetValue(Grid.ColumnProperty)) = 1 Select child)(0), TextBlock)
        Dim l = DirectCast((From child In StepsGrid.Children Where CInt(DirectCast(child, UIElement).GetValue(Grid.RowProperty)) = CurrentStep AndAlso CInt(DirectCast(child, UIElement).GetValue(Grid.ColumnProperty)) = 0 Select child)(0), Image)

        tb.FontWeight = FontWeights.Normal
        Dim uriSource = New Uri("Resources\iconSkip.png", UriKind.Relative)
        l.Source = New BitmapImage(uriSource)

        CurrentStep += 1
    End Sub
    Delegate Sub SkipDelegate()

    Private Sub _Success()
        ProgBar.Value = ProgBar.Maximum

        StatuTextBox.Text = "Porting complete!"
        StatuTextBox.Foreground = Brushes.Green
        StatuTextBox.FontWeight = FontWeights.Bold
        OkButton.IsEnabled = True
    End Sub
    Delegate Sub SuccessDelegate()

    Private Sub _CurrentStepFailed(reason As String, fullError As String)
        Dim tb = DirectCast((From child In StepsGrid.Children Where CInt(DirectCast(child, UIElement).GetValue(Grid.RowProperty)) = CurrentStep AndAlso CInt(DirectCast(child, UIElement).GetValue(Grid.ColumnProperty)) = 1 Select child)(0), TextBlock)
        Dim l = DirectCast((From child In StepsGrid.Children Where CInt(DirectCast(child, UIElement).GetValue(Grid.RowProperty)) = CurrentStep AndAlso CInt(DirectCast(child, UIElement).GetValue(Grid.ColumnProperty)) = 0 Select child)(0), Image)

        Dim uriSource = New Uri("Resources\iconCross.png", UriKind.Relative)
        l.Source = New BitmapImage(uriSource)

        StatuTextBox.Text = "FATAL ERROR: " & reason
        StatuTextBox.Foreground = Brushes.Red
        OkButton.IsEnabled = True
    End Sub
    Delegate Sub CurrentStepFailedDelegate(r As String, f As String)

    Private Sub ButtonOk_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Me.Close()
    End Sub
End Class
